/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

import {formatMessage} from './intl';
import {partOrderPriceTerms} from '../Enum';
export const renderBoolean = value => {
    if(typeof value !== 'boolean') return value;
    else if(value)
        return formatMessage({
            id: 'utils.yes',
            defaultMessage: '是'
        });
    return formatMessage({
        id: 'utils.no',
        defaultMessage: '否'
    });
};
export const validateAdd = (data, id) => {
    const {
        dealerId,
        brandId,
        buyer,
        buyerAddress,
        buyerNumber,
        buyerCantact,
        seller,
        sellerAddress,
        sellerNumber,
        origin,
        destination,
        paymentType,
        priceTerms,
        shipmentType,
        freight,
        insurance,
        isBonusNumber,
        discountReason,
        packingLists,
        totalDiscount,
        totalFee
    } = data;
    const msgs = [];
    if(!dealerId && !id)
        msgs.push(
            formatMessage({
                id: 'utils.dealerId',
                defaultMessage: '总代'
            })
        );
    if(!brandId && !id)
        msgs.push(
            formatMessage({
                id: 'utils.brandId',
                defaultMessage: '品牌'
            })
        );
    if(!buyer)
        msgs.push(
            formatMessage({
                id: 'utils.buyer',
                defaultMessage: '买方名称'
            })
        );
    if(!buyerAddress)
        msgs.push(
            formatMessage({
                id: 'utils.buyerAddress',
                defaultMessage: '买方地址'
            })
        );
    if(!buyerNumber)
        msgs.push(
            formatMessage({
                id: 'utils.buyerNumber',
                defaultMessage: '联系电话'
            })
        );
    if(!buyerCantact)
        msgs.push(
            formatMessage({
                id: 'utils.buyerCantact',
                defaultMessage: '买方联系人'
            })
        );
    if(!seller)
        msgs.push(
            formatMessage({
                id: 'utils.seller',
                defaultMessage: '卖方名称'
            })
        );
    if(!sellerAddress)
        msgs.push(
            formatMessage({
                id: 'utils.sellerAddress',
                defaultMessage: '卖方地址'
            })
        );
    if(!sellerNumber)
        msgs.push(
            formatMessage({
                id: 'utils.sellerNumber',
                defaultMessage: '卖方电话'
            })
        );
    if(!origin)
        msgs.push(
            formatMessage({
                id: 'utils.origin',
                defaultMessage: '起运地'
            })
        );
    if(!destination)
        msgs.push(
            formatMessage({
                id: 'utils.destination',
                defaultMessage: '目的地'
            })
        );
    if(!paymentType)
        msgs.push(
            formatMessage({
                id: 'utils.paymentType',
                defaultMessage: '付款方式'
            })
        );
    if(!priceTerms)
        msgs.push(
            formatMessage({
                id: 'utils.priceTerms',
                defaultMessage: '价格条款'
            })
        );
    if(!shipmentType)
        msgs.push(
            formatMessage({
                id: 'utils.shipmentType',
                defaultMessage: '运输方式'
            })
        );
    if((priceTerms === partOrderPriceTerms.CIF ||
        priceTerms === partOrderPriceTerms.CFR ||
        priceTerms === partOrderPriceTerms.CIP ||
        priceTerms === partOrderPriceTerms.CPT) &&
        !freight)
        msgs.push(
            formatMessage({
                id: 'utils.freight',
                defaultMessage: '运费'
            })
        );
    if((priceTerms === partOrderPriceTerms.CIF ||
        priceTerms === partOrderPriceTerms.CIP) && !insurance)
        msgs.push(
            formatMessage({
                id: 'utils.insurance',
                defaultMessage: '保费'
            })
        );
    if(typeof isBonusNumber !== 'number')
        msgs.push(
            formatMessage({
                id: 'utils.isBonus',
                defaultMessage: '是否使用返利'
            })
        );
    if(!discountReason && totalDiscount)
        msgs.push(
            formatMessage({
                id: 'utils.discountReason',
                defaultMessage: '折扣原因'
            })
        );
    if(!packingLists.length)
        msgs.push(
            formatMessage({
                id: 'utils.packingLists',
                defaultMessage: '箱单发票装箱单'
            })
        );
    if(msgs.length)
        return `${msgs.join(
            formatMessage({
                id: 'utils.comma',
                defaultMessage: '、'
            })
        )}${formatMessage({
            id: 'utils.notEmpty',
            defaultMessage: '不能为空'
        })}`;
    if(totalFee <= 0)
        return formatMessage({
            id: 'utils.totalFee',
            defaultMessage: '总金额必须大于0'
        });
    return false;
};
